package org.melord.cap;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Popup extends Thread {

	Shell shell;

	protected int moveStep = 2; // 
	protected int upPosition; // 
	protected int downPosition; // 
	protected int leftPosition;// 

	int time = 5; // s

	int m = time / 60;
	int s = time % 60;

	long cur;

	Label down;
	Host host;

	private Callback callback;

	public Popup(final Host host, final Callback callback) {
		this.host = host;
		this.callback = callback;
		shell = new Shell(SWT.ON_TOP | SWT.SYSTEM_MODAL);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		OS.SetWindowPos(shell.handle, OS.HWND_TOPMOST, 500, 111, 1000, 200,
				SWT.NULL);
		// Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
		// text.setBounds(10, 20, 180, 80);
		// text.setBackground(shell.getBackground());
		// text.setText(message);
		Label label1 = new Label(shell, SWT.NONE);
		label1.setText("Hello!!!");

		Label label2 = new Label(shell, SWT.NONE);
		label2.setText(String.format("倒计时 %s:%s", m, s));

		down = new Label(shell, SWT.NONE);
		down.setText(String.format("%2d:%2d", m, s));

		cur = System.currentTimeMillis();

		// ȡ��Ī��С
		Rectangle area = Display.getDefault().getClientArea();

		upPosition = area.height - 100;// �����popup��������Ļ��ʾ�����λ��
		downPosition = area.height + 100;// �����popup����ĳ�ʼλ��
		leftPosition = area.width - 180;

		shell.setSize(180, 100);

		// ��ʼ��popupλ��
		shell.setLocation(leftPosition, downPosition);

		shell.open();
		

		final Timer update = new Timer();
		TimerTask updateTask = new TimerTask() {

			@Override
			public void run() {
				System.out.println("Update timer task");
				shell.getDisplay().asyncExec(new Runnable() {
					public void run() {
						long diff = System.currentTimeMillis() - cur;
						int temp = time - (int) diff / 1000;
						int tempm = temp / 60;
						int temps = temp % 60;
						down.setText(String.format("%2d:%2d", tempm, temps));
					}
				});
			}
		};
		update.scheduleAtFixedRate(updateTask, 100, 300);

		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			public void run() {
				System.out.println("Exit timer task");
				update.cancel();
				shell.getDisplay().asyncExec(new Runnable() {
					public void run() {
						shell.dispose();
						if(callback != null){
							callback.onDismiss(host);
						}
					}
				});
			}
		};
		timer.schedule(task, time * 1000);
	}

	public void run() {

		Display display = shell.getDisplay();
		while (true) {
			try {
				Thread.sleep(10);

				// �жϵ�ǰλ���Ƿ�С���ܳ��ֵ����λ�ã�С�ڵĻ���˵�������������ƶ���
				if ((downPosition - moveStep) > upPosition && !display.isDisposed()) {
					display.asyncExec(new Runnable() {
						public void run() {
							shell.setLocation(leftPosition, downPosition
									- moveStep);
							downPosition -= moveStep;
						}
					});
					// ��ʱ�Ѿ��ƶ��������λ�ã���ʾ5���Ӻ󣬹رմ��ڲ��˳���
				} else {
					// Thread.sleep(5000);
					// display.asyncExec(new Runnable() {
					// public void run() {
					// shell.dispose();
					// }
					// });
					break;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("exit thread");
	}

	public void setCallback(Callback callback) {
		this.callback = callback;
	}

	public static interface Callback {
		void onDismiss(Host host);
	}
}